
package com.cn.kabalifemerchants.ui.login;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cn.kabalifemerchants.R;
import com.cn.kabalifemerchants.api.Api;
import com.cn.kabalifemerchants.base.IBaseActivity;
import com.cn.kabalifemerchants.callback.CacheCallBack;
import com.cn.kabalifemerchants.callback.SchedulerTransformer;
import com.cn.kabalifemerchants.config.Constants;
import com.cn.kabalifemerchants.config.LoginHelper;
import com.cn.kabalifemerchants.event.RxBus;
import com.cn.kabalifemerchants.event.RxBusBean;
import com.cn.kabalifemerchants.ui.login.AgreementActivity;
import com.cn.kabalifemerchants.ui.login.CooperationPresenter;
import com.cn.kabalifemerchants.ui.login.CooperationSreviceActivity;
import com.cn.kabalifemerchants.ui.login.CooperationView;
import com.cn.kabalifemerchants.ui.login.adapter.AddressCheckAdapter;
import com.cn.kabalifemerchants.ui.login.adapter.GridImageAdapter;
import com.cn.kabalifemerchants.ui.login.bean.AddressProvinceListBean;
import com.cn.kabalifemerchants.ui.login.bean.MsgBean;
import com.cn.kabalifemerchants.ui.page.bean.UpImgBean;
import com.cn.kabalifemerchants.utils.GlideEngine;
import com.cn.kabalifemerchants.utils.LoggerUtils;
import com.cn.kabalifemerchants.utils.RxToast;
import com.cn.kabalifemerchants.utils.SoftHideKeyBoardUtil;
import com.cn.kabalifemerchants.utils.StringUtil;
import com.cn.kabalifemerchants.utils.TimeUtils;
import com.cn.kabalifemerchants.view.PopupWindow.CommonPopupWindow;
import com.cn.kabalifemerchants.view.PopupWindow.CommonUtil;
import com.flyco.roundview.RoundTextView;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.functions.Action1;

/**
 * Created by 晓林 on 2020/9/7.
 * function: 合作
 */
public class CooperationActivity extends IBaseActivity<CooperationView, CooperationPresenter> implements CooperationView, CommonPopupWindow.ViewInterface {


    String ids;//返回的服务类型id
    String idsImg;//返回的图片
    @BindView(R.id.img_top)
    ImageView imgTop;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.img_check_1)
    ImageView imgCheck1;
    @BindView(R.id.tv_str_1)
    TextView tvStr1;
    @BindView(R.id.ll_btn_1)
    LinearLayout llBtn1;
    @BindView(R.id.img_check_2)
    ImageView imgCheck2;
    @BindView(R.id.tv_str_2)
    TextView tvStr2;
    @BindView(R.id.ll_btn_2)
    LinearLayout llBtn2;
    @BindView(R.id.img_check_3)
    ImageView imgCheck3;
    @BindView(R.id.tv_str_3)
    TextView tvStr3;
    @BindView(R.id.ll_btn_3)
    LinearLayout llBtn3;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_card)
    TextView tvCard;
    @BindView(R.id.et_card)
    EditText etCard;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_address2)
    TextView tvAddres2;
    @BindView(R.id.tv_address3)
    TextView tvAddres3;
    @BindView(R.id.img_address1)
    ImageView img_address1;
    @BindView(R.id.img_address2)
    ImageView img_address2;
    @BindView(R.id.img_address3)
    ImageView img_address3;
    @BindView(R.id.ll_btn_address)
    LinearLayout llBtnAddress;
    @BindView(R.id.ll_btn_address2)
    LinearLayout llBtnAddres2;
    @BindView(R.id.ll_btn_address3)
    LinearLayout llBtnAddress3;
    @BindView(R.id.tv_service)
    TextView tvService;
    @BindView(R.id.ll_btn_service)
    LinearLayout llBtnService;
    @BindView(R.id.tv_user_name)
    EditText tvUserName;
    @BindView(R.id.tv_phone)
    EditText tvPhone;
    @BindView(R.id.tv_ph_address)
    TextView tvPhAddress;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.img_check)
    ImageView imgCheck;
    @BindView(R.id.tv_btn_agreement)
    TextView tvBtnAgreement;
    @BindView(R.id.tv_btn_next)
    RoundTextView tvBtnNext;
    @BindView(R.id.ll_left)
    RelativeLayout llLeft;
    @BindView(R.id.recyclerviewpic)
    RecyclerView recyclerviewpic;
    @BindView(R.id.img_1)
    ImageView img_1;
    @BindView(R.id.img_2)
    ImageView img_2;
    @BindView(R.id.img_3)
    ImageView img_3;
    private List<LocalMedia> selectList = new ArrayList<>();
    private int maxSelectNum = 3;
    private GridImageAdapter adapter;
    private int themeId;
    List<String> temListPicUrl = new ArrayList<>();//临时保存图片的地址，要比对上传过的是否存在。
    List<String> strPics = new ArrayList<>();
    List<String> list = new ArrayList<>();
    List<String> listIds = new ArrayList<>();



    public final static int CHOOSE_PHOTO_1 = 1001;
    public final static int CHOOSE_PHOTO_2 = 1002;
    public final static int CHOOSE_PHOTO_3 = 1003;
    private List<LocalMedia> select1 = new ArrayList<>();
    private List<LocalMedia> select2 = new ArrayList<>();
    private List<LocalMedia> select3 = new ArrayList<>();
    String pathUrl1 = "";//
    String pathUrl2 = "";//
    String pathUrl3 = "";//
    int signImg=0;//标示是上传那个。





    private CommonPopupWindow popupWindowAddress;
    List<AddressProvinceListBean.ResultBean> mListCitys;
    AddressCheckAdapter addressCheckAdapter;
    boolean isIsgn = true;
    int signAddress = 1;//当前的级数
    String province,
            city,
            area,
            street;
    int address1 = 0;  //当前级数的对应的id
    int address2 = 0;
    int address3 = 0;
    int address4 = 0;

    int address11 = 0;  //当前级数的对应的id
    int address22 = 0;
    int address33 = 0;
    int address44 = 0;

    int address111 = 0;  //当前级数的对应的id
    int address222 = 0;
    int address3333= 0;
    int address4444 = 0;

    int address1ph = 0;  //当前级数的对应的id
    int address2ph = 0;
    int address3ph= 0;
    int address4ph = 0;
    public final static int onActivityResult_To_ComName = 1010;


    boolean isCheck = false;

    int  identity=1; //1：公司，2：个体工商户，3：个人

    int addressSign;//标示的哪里点击的地址。
    @Override
    protected CooperationPresenter createPresenter() {
        return new CooperationPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_cooperation;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        SoftHideKeyBoardUtil.assistActivity(this);
        themeId = R.style.picture_default_style;
        recyclerviewpic.setLayoutManager(new GridLayoutManager(getMe(), 3));
        adapter = new GridImageAdapter(getMe(), onAddPicClickListener,2);
        adapter.setList(selectList);
        adapter.setSelectMax(maxSelectNum);
        recyclerviewpic.setAdapter(adapter);
        adapter.setOnItemClickListener(new GridImageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                if (selectList.size() > 0) {
                    LocalMedia media = selectList.get(position);
//                    LoggerUtils.e("这是：" + media.getPath() + media.getCompressPath() + media.getCutPath());
                    String mimeType = media.getMimeType();
                    int mediaType = PictureMimeType.getMimeType(mimeType);
                    switch (mediaType) {
                        case PictureConfig.TYPE_VIDEO:
                            // 预览视频
                            PictureSelector.create(CooperationActivity.this).externalPictureVideo(media.getPath());
                            break;
                        case PictureConfig.TYPE_AUDIO:
                            // 预览音频
                            PictureSelector.create(CooperationActivity.this).externalPictureAudio(media.getPath());
                            break;
                        default:
                            // 预览图片 可自定长按保存路径
                            PictureSelector.create(CooperationActivity.this)
                                    .themeStyle(R.style.picture_default_style) // xml设置主题
                                    //.setPictureStyle(mPictureParameterStyle)// 动态自定义相册主题
                                    //.setPictureWindowAnimationStyle(animationStyle)// 自定义页面启动动画
                                    .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)// 设置相册Activity方向，不设置默认使用系统
                                    .isNotPreviewDownload(true)// 预览图片长按是否可以下载
                                    .loadImageEngine(GlideEngine.createGlideEngine())// 外部传入图片加载引擎，必传项
                                    .openExternalPreview(position, selectList);
                            break;
                    }
                }
            }
        });
        rxBusSubscription = RxBus.getDefault().toObservable(RxBusBean.class)
                .compose(new SchedulerTransformer<RxBusBean>())
                .subscribe(new Action1<RxBusBean>() {
                    @SuppressLint("WrongConstant")
                    @Override
                    public void call(RxBusBean rxBusBean) {
                        if (rxBusBean.getId() == 204 && rxBusBean.getSign() == 204) {
                            // 删除图片后才传值
                            int index = (int) rxBusBean.getObjBean();
//                            LoggerUtils.e("删除了图片收到之前集合：" + strPics.size() + "str：" + strPics.toString());
//                            LoggerUtils.e("删除了图片收到：" + index);
                            temListPicUrl.remove(index);// 删除临时集合里面的
//                            RxToast.success("这边注释掉一行代码,删除记得释放");
                            strPics.remove(index); // 删除要上传id集合里面的
//                            LoggerUtils.e("删除了图片收到之后集合：" + strPics.size() + "str：" + strPics.toString());

                        }
                    }
                });

        init(1);//默认公司
    }
    private GridImageAdapter.onAddPicClickListener onAddPicClickListener = new GridImageAdapter.onAddPicClickListener() {
        @Override
        public void onAddPicClick() {
            // 进入相册 以下是例子：不需要的api可以不写
            PictureSelector.create(CooperationActivity.this)
                    .openGallery(PictureMimeType.ofImage())// 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                    .loadImageEngine(GlideEngine.createGlideEngine())// 外部传入图片加载引擎，必传项
                    .theme(themeId)// 主题样式设置 具体参考 values/styles   用法：R.style.picture.white.style v2.3.3后 建议使用setPictureStyle()动态方式
                    .isWeChatStyle(false)// 是否开启微信图片选择风格
                    .isWithVideoImage(true)// 图片和视频是否可以同选,只在ofAll模式下有效
                    .maxSelectNum(maxSelectNum)// 最大图片选择数量
                    .minSelectNum(1)// 最小选择数量
                    .imageSpanCount(3)// 每行显示个数
                    .isReturnEmpty(false)// 未选择数据时点击按钮是否可以返回
                    .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)// 设置相册Activity方向，不设置默认使用系统
                    .isOriginalImageControl(false)// 是否显示原图控制按钮，如果设置为true则用户可以自由选择是否使用原图，压缩、裁剪功能将会失效
                    .selectionMode(
                            PictureConfig.MULTIPLE)// 多选 or 单选
                    .isSingleDirectReturn(false)// 单选模式下是否直接返回，PictureConfig.SINGLE模式下有效
                    .previewImage(true)// 是否可预览图片
                    .previewVideo(false)// 是否可预览视频
                    .enablePreviewAudio(false) // 是否可播放音频
                    .isCamera(true)// 是否显示拍照按钮
                    //.isMultipleSkipCrop(false)// 多图裁剪时是否支持跳过，默认支持
                    .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                    //.imageFormat(PictureMimeType.PNG)// 拍照保存图片格式后缀,默认jpeg
                    .enableCrop(false)// 是否裁剪
                    .compress(true)// 是否压缩
                    .compressQuality(80)// 图片压缩后输出质量 0~ 100
                    .synOrAsy(true)//同步false或异步true 压缩 默认同步
                    .isGif(false)// 是否显示gif图片
                    .openClickSound(true)// 是否开启点击声音
                    .selectionMedia(selectList)// 是否传入已选图片
                    .cutOutQuality(90)// 裁剪输出质量 默认100
                    .minimumCompressSize(100)// 小于100kb的图片不压缩
                    .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
        }

    };
    @OnClick({R.id.img_1,R.id.img_2,R.id.img_3,R.id.tv_btn_agreement, R.id.ll_left, R.id.img_check, R.id.ll_btn_address,
            R.id.ll_btn_address2,R.id.ll_btn_address3,R.id.img_address1,R.id.img_address2,R.id.img_address3,R.id.img_b_1,R.id.img_b_2,R.id.img_b_3,
            R.id.ll_btn_service, R.id.tv_btn_next,R.id.ll_btn_1, R.id.ll_btn_2, R.id.ll_btn_3,R.id.ll_btn_ph_address})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_b_1:
                pathUrl1 = "";
                img_1.setImageResource(R.drawable.sfz_z);
                break;
            case R.id.img_b_2:
                pathUrl2 = "";
                img_1.setImageResource(R.drawable.sfz_f);
                break;
            case R.id.img_b_3:
                pathUrl2 = "";
                if (identity==3){
                    img_3.setImageResource(R.drawable.shefnez);
                }else img_3.setImageResource(R.drawable.yingye);
                break;
            case  R.id.img_1:
                imgD(CHOOSE_PHOTO_1);
                break;
            case R.id.img_2:
                imgD(CHOOSE_PHOTO_2);
                break;
            case R.id.img_3:
                imgD(CHOOSE_PHOTO_3);
                break;
            case R.id.tv_btn_agreement:
                Bundle bundle = new Bundle();
                bundle.putInt("sign", 3);
                $startActivity(AgreementActivity.class, bundle);
                break;
            case R.id.ll_left:
                hideKeyboard(etAddress);
                finish();
                break;
            case R.id.ll_btn_address:
                //省
                hideKeyboard(etAddress);
                isIsgn = true;
                addressSign=1;
                signAddress = 1;
                mPresenter.onProvinceListData();
//                if (!TextUtils.isEmpty(tvAddress.getText().toString().trim())) {
//                    signAddress = 4;
//                    mPresenter.onCityListData(address3);
//                } else {
//                    signAddress = 1;
//                    mPresenter.onProvinceListData();
//                }
                break;

            case R.id.ll_btn_address2:
                hideKeyboard(etAddress);
                isIsgn = true;
                addressSign=2;
                signAddress = 1;
                mPresenter.onProvinceListData();
                break;
            case R.id.ll_btn_address3:
                hideKeyboard(etAddress);
                isIsgn = true;
                addressSign=3;
                signAddress = 1;
                mPresenter.onProvinceListData();
                break;
            case R.id.img_address1:
                if (!TextUtils.isEmpty(tvAddress.getText().toString().trim()) &&TextUtils.isEmpty(tvAddres2.getText().toString().trim()))
                {
                    llBtnAddres2.setVisibility(View.VISIBLE);
                }else  if (!TextUtils.isEmpty(tvAddress.getText().toString().trim()) &&!TextUtils.isEmpty(tvAddres2.getText().toString().trim()))
                {
                    llBtnAddress3.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.img_address2:
                tvAddres2.setText("");
                llBtnAddres2.setVisibility(View.GONE);
                break;
            case R.id.img_address3:
                tvAddres3.setText("");
                llBtnAddress3.setVisibility(View.GONE);
                break;
            case R.id.ll_btn_service:
                Intent intent = new Intent(getMe(), CooperationSreviceActivity.class);
                intent.putExtra("ids", ids);
                intent.putExtra("sign", 1);
                startActivityForResult(intent, onActivityResult_To_ComName);
                break;

            case R.id.img_check:
                if (isCheck) {
                    imgCheck.setImageResource(R.drawable.check_false);
                    isCheck = false;
                } else {
                    imgCheck.setImageResource(R.drawable.check_true);
                    isCheck = true;
                }
                break;
            case R.id.ll_btn_1:
                init(1);
                break;
            case R.id.ll_btn_2:
                init(2);
                break;
            case R.id.ll_btn_3:
                init(3);
                break;
            case R.id.ll_btn_ph_address:
                //联系地址
                hideKeyboard(etAddress);
                isIsgn = true;
                addressSign=4;
                signAddress = 1;
                mPresenter.onProvinceListData();
//                if (!TextUtils.isEmpty(tvPhAddress.getText().toString().trim())) {
//                    signAddress = 4;
//                    mPresenter.onCityListData(address3);
//                } else {
//                    signAddress = 1;
//                    mPresenter.onProvinceListData();
//                }
                break;
            case R.id.tv_btn_next:
//
                if (!isCheck)
                {   RxToast.error("请同意协议");
                    break;
                }
                if (TextUtils.isEmpty(etName.getText().toString().trim())) {
                    RxToast.error(identity==3?"请输入姓名":"请输入公司名称");
                    break;
                }
                if (TextUtils.isEmpty(etCard.getText().toString().trim())) {
                    RxToast.error(identity==3?"请输入个人(身份证号)":identity==1?"请输入公司(信用代码)":"请输入个体工商(注册号)");
                    break;
                }
                if (TextUtils.isEmpty(tvAddress.getText().toString().trim())) {
                    RxToast.error("请选择服务区域");
                    break;
                }
                if (TextUtils.isEmpty(tvService.getText().toString().trim())) {
                    RxToast.error("请选择服务类型");
                    break;
                }
                if (TextUtils.isEmpty(tvUserName.getText().toString().trim())) {
                    RxToast.error("请输入联系人");
                    break;
                }
                if (TextUtils.isEmpty(tvPhone.getText().toString().trim())) {
                    RxToast.error("请輸入联系电话");
                    break;
                }
                if (!StringUtil.isChinaPhoneLegal(tvPhone.getText().toString().trim())) {
                    RxToast.error("请输入正确的手机号");
                    break;
                }
                if (TextUtils.isEmpty(etAddress.getText().toString().trim())) {
                    RxToast.error("请输入详情地址");
                    break;
                }
                if (TextUtils.isEmpty(tvPhAddress.getText().toString().trim())) {
                    RxToast.error("请选择联系地址");
                    break;
                }

                if (TextUtils.isEmpty(pathUrl1)) {
                    RxToast.error("请上传身份证正面");
                    break;
                }
                if (TextUtils.isEmpty(pathUrl2)) {
                    RxToast.error("请上传身份证反面");
                    break;
                }
                if (TextUtils.isEmpty(pathUrl3)) {
                    RxToast.error(identity==3?"请上传手持身份证":"请上传营业执照");
                    break;
                }
                if (listIds.size() > 0) {
                    idsImg=  StringUtil.convertListToString(listIds);
                    LoggerUtils.e("其他资质的图片："+idsImg);
                }
//                LoggerUtils.e("数据：服务区域1="+tvAddress.getText().toString().trim()+"对应的id："+address4);
//                LoggerUtils.e("数据：服务区域2="+tvAddres2.getText().toString().trim()+"对应的id："+address44);
//                LoggerUtils.e("数据：服务区域3="+tvAddres3.getText().toString().trim()+"对应的id："+address4444);

                List<String> result=new ArrayList<>();
                if (!TextUtils.isEmpty(idsImg))
                    result = Arrays.asList(idsImg.split(","));
                String  region="";
                if (!TextUtils.isEmpty(tvAddress.getText().toString().trim()) && !TextUtils.isEmpty(tvAddres2.getText().toString().trim())) {
                    region=address3+","+address33;
                }else if (!TextUtils.isEmpty(tvAddress.getText().toString().trim()) && !TextUtils.isEmpty(tvAddres2.getText().toString().trim())&& !TextUtils.isEmpty(tvAddres3.getText().toString().trim())) {
                    region=address3+","+address33+","+address3333;
                }else region=address3+"";
                LoggerUtils.e("数据：类型="+identity);
                LoggerUtils.e("数据：名称="+etName.getText().toString().trim());
                LoggerUtils.e("数据：号码="+etCard.getText().toString().trim());
                LoggerUtils.e("数据：服务区域1="+tvAddress.getText().toString().trim()+"对应的区id：="+address4);
                LoggerUtils.e("数据：服务区域2="+tvAddres2.getText().toString().trim()+"对应的区id：="+address44);
                LoggerUtils.e("数据：服务区域3="+tvAddres3.getText().toString().trim()+"对应的区id：="+address4444);
                LoggerUtils.e("数据：服务类型="+tvService.getText().toString().trim());
                LoggerUtils.e("数据：联系人="+tvUserName.getText().toString().trim());
                LoggerUtils.e("数据：联系电话="+tvPhone.getText().toString().trim());
                LoggerUtils.e("数据：联系地址="+tvPhAddress.getText().toString().trim()+"对应的id：="+address1ph+"="+address2ph+"="+address3ph+"="+address4ph);
                LoggerUtils.e("数据：详细地址="+etAddress.getText().toString().trim());
                LoggerUtils.e("数据：身份正面="+pathUrl1);
                LoggerUtils.e("数据：身份反面="+pathUrl2);
                LoggerUtils.e("数据：营业执照="+pathUrl3);
                LoggerUtils.e("数据：其他资质的图片："+idsImg);
                LoggerUtils.e("数据：最后上传的区域："+region);

                mPresenter.onUserBusinessData(address1ph, address2ph, address3ph, address4ph,
                        etAddress.getText().toString().trim(),
                        ids, etName.getText().toString().trim(), tvPhone.getText().toString().trim(),
                        result, tvUserName.getText().toString().trim(),identity,etCard.getText().toString(),region,pathUrl1,pathUrl2,pathUrl3);

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case RESULT_OK:
                switch (requestCode) {
                    case CHOOSE_PHOTO_1:
                        // 图片选择结果回调
                        select1 = PictureSelector.obtainMultipleResult(data);
                        for (LocalMedia media : select1) {
                            String url="";
                            if (media.isCut() && !media.isCompressed()) {
                                // 裁剪过
                                url = media.getCutPath();
                            } else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
                                url = media.getCompressPath();
                            } else {
                                // 原图
                                url = media.getPath();
                            }
                            Glide.with(this).load(url).into(img_1);
                            //todo 身份证正面
                            signImg=1;
                            mPresenter.onUpImgData(url);
                        }
                        break;
                    case CHOOSE_PHOTO_2:
                        // 图片选择结果回调
                        select2 = PictureSelector.obtainMultipleResult(data);
                        for (LocalMedia media : select2) {
                            String url="";
                            if (media.isCut() && !media.isCompressed()) {
                                // 裁剪过
                                url = media.getCutPath();
                            } else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
                                url = media.getCompressPath();
                            } else {
                                // 原图
                                url = media.getPath();
                            }
                            Glide.with(this).load(url).into(img_2);
                            //todo 身份证正面
                            signImg=2;
                            mPresenter.onUpImgData(url);
                        }
                        break;
                    case CHOOSE_PHOTO_3:
                        // 图片选择结果回调
                        select3 = PictureSelector.obtainMultipleResult(data);
                        for (LocalMedia media : select3) {
                            String url="";
                            if (media.isCut() && !media.isCompressed()) {
                                // 裁剪过
                                url = media.getCutPath();
                            } else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
                                url = media.getCompressPath();
                            } else {
                                // 原图
                                url = media.getPath();
                            }
                            Glide.with(this).load(url).into(img_3);
                            //todo 身份证正面
                            signImg=3;
                            mPresenter.onUpImgData(url);
                        }
                        break;
                    case PictureConfig.CHOOSE_REQUEST:
                        //这是上传其他资质
                        selectList = PictureSelector.obtainMultipleResult(data);
                        adapter.setList(selectList);
                        adapter.notifyDataSetChanged();
                        if (temListPicUrl.size() > 0) {
                            for (LocalMedia media : selectList) {
                                Log.i("压缩:", media.getCompressPath());
                                if (temListPicUrl.contains(media.getCompressPath())) {
                                    //如果存在 不管
                                } else {
                                    //如果不存在的话就添加和上传上传
                                    temListPicUrl.add(media.getCompressPath());
                                    signImg=0;
                                    mPresenter.onUpImgData(media.getCompressPath());
                                }
                            }
                        } else {
                            for (int i = 0; i < selectList.size(); i++) {
                                temListPicUrl.add(selectList.get(i).getCompressPath());
//                               Presenter.onUpImgData(selectList.get(i).getCompressPath());
                                list.add(selectList.get(i).getCompressPath());
                            }
                            upAdFile();
                        }
                        break;

                }
                break;
            case onActivityResult_To_ComName:
                if (data != null) {
                    String names = data.getStringExtra("names");
                    ids = data.getStringExtra("ids");
                    tvService.setText(names);
                }
                break;
        }
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return getMe();
    }

    @Override
    public void onUpImgSuccess(UpImgBean bean) {
        if (signImg==1){
            pathUrl1=bean.getResult().get(0);
        }else  if (signImg==2){
            pathUrl2=bean.getResult().get(0);
        }else  if (signImg==3){
            pathUrl3=bean.getResult().get(0);
        }else {
            listIds.add(bean.getResult().get(0));
        }

    }

    @Override
    public void onProvinceListSuccess(AddressProvinceListBean bean) {
        if (bean.getResult() != null && bean.getResult().size() > 0) {
            mListCitys = bean.getResult();
            showAddress();
        }
    }

    @Override
    public void onCityListSuccess(AddressProvinceListBean bean) {
        if (bean.getResult() != null && bean.getResult().size() > 0) {
            mListCitys = bean.getResult();
            if (!isIsgn)
                addressCheckAdapter.replaceData(mListCitys);
            else showAddress();
        }
    }

    @Override
    public void onUserBusinessSuccess(MsgBean bean) {
        RxToast.success(bean.getMsg());
        finish();
    }

    /**
     * 优惠劵
     */
    private void showAddress() {
        if (popupWindowAddress != null && popupWindowAddress.isShowing()) return;
        View upView = LayoutInflater.from(getMe()).inflate(R.layout.pw_address, null);
        //测量View的宽高
        CommonUtil.measureWidthAndHeight(upView);
        popupWindowAddress = new CommonPopupWindow.Builder(getMe())
                .setView(R.layout.pw_address)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, upView.getMeasuredHeight())
                .setBackGroundLevel(0.5f)//取值范围0.0f-1.0f 值越小越暗
                .setAnimationStyle(R.style.AnimUp)
                .setViewOnclickListener(this)
                .create();
        popupWindowAddress.showAtLocation(findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void getChildView(View view, int layoutResId) {
        switch (layoutResId) {
            //todo
            case R.layout.pw_address:
                RecyclerView rvCoupon = view.findViewById(R.id.recyclerView);
                rvCoupon.setLayoutManager(new LinearLayoutManager(getMe()));
                TextView tv_1 = view.findViewById(R.id.tv_1);
                TextView tv_2 = view.findViewById(R.id.tv_2);
                TextView tv_3 = view.findViewById(R.id.tv_3);
                TextView tv_4 = view.findViewById(R.id.tv_4);
//                if (!TextUtils.isEmpty(province)) {
//                    tv_1.setText(province);
//                    tv_1.setVisibility(View.VISIBLE);
//                }
//                if (!TextUtils.isEmpty(city)) {
//                    tv_2.setVisibility(View.VISIBLE);
//                    tv_2.setText(city);
//                }
//
//                if (!TextUtils.isEmpty(area)) {
//                    tv_3.setVisibility(View.VISIBLE);
//                    tv_3.setText(area);
//                }
//
//                if (!TextUtils.isEmpty(street)) {
//                    tv_4.setVisibility(View.VISIBLE);
//                    tv_4.setText(street);
//                }
                tv_1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.isEmpty(tv_2.getText().toString().toString())) {
                            tv_1.setText("");
                            tv_1.setVisibility(View.GONE);
                            signAddress = 1;
                            province = "";
                            tvAddress.setText("");
                            isIsgn = false;
                            address1 = 0;
                            mPresenter.onCityListData(1);



                        }

                    }
                });
                tv_2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.isEmpty(tv_3.getText().toString().toString())) {
                            tv_2.setText("");
                            tv_2.setVisibility(View.GONE);
                            signAddress = 2;
                            city = "";
                            isIsgn = false;
                            if (addressSign==1){
                                address2 = 0;
                                mPresenter.onCityListData(address1);
                            }else  if (addressSign==2){
                                address22 = 0;
                                mPresenter.onCityListData(address11);
                            }else  if (addressSign==3){
                                address222 = 0;
                                mPresenter.onCityListData(address111);
                            }else  if (addressSign==4){
                                address2ph = 0;
                                mPresenter.onCityListData(address1ph);
                            }
                        }
                    }
                });
                tv_3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (TextUtils.isEmpty(tv_4.getText().toString().toString())) {
                            tv_3.setText("");
                            tv_3.setVisibility(View.GONE);
                            signAddress = 3;
                            area = "";
                            isIsgn = false;
                            if (addressSign==1){
                                address3 = 0;
                                mPresenter.onCityListData(address2);
                            }else  if (addressSign==2){
                                address33= 0;
                                mPresenter.onCityListData(address22);
                            }else  if (addressSign==3){
                                address3333 = 0;
                                mPresenter.onCityListData(address222);
                            }else  if (addressSign==4){
                                address3ph = 0;
                                mPresenter.onCityListData(address2ph);
                            }
                        }
                    }
                });
                tv_4.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tv_4.setText("");
                        tv_4.setVisibility(View.GONE);
                        signAddress = 4;
                        street = "";
                        isIsgn = false;
                        if (addressSign==1){
                            address4 = 0;
                            mPresenter.onCityListData(address3);
                        }else  if (addressSign==2){
                            address44 = 0;
                            mPresenter.onCityListData(address33);
                        }else  if (addressSign==3){
                            address4444 = 0;
                            mPresenter.onCityListData(address3333);
                        }else  if (addressSign==4){
                            address4ph = 0;
                            mPresenter.onCityListData(address3ph);
                        }


                    }
                });
                //创建适配器
                addressCheckAdapter = new AddressCheckAdapter(R.layout.pw_address_item, mListCitys);
                //给RecyclerView设置适配器
                rvCoupon.setAdapter(addressCheckAdapter);
                addressCheckAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        if (signAddress == 1) {
                            tv_1.setVisibility(View.VISIBLE);
                            tv_1.setText(addressCheckAdapter.getItem(position).getName());
                            if (addressSign==1)
                                address1 = addressCheckAdapter.getItem(position).getId();
                            else if (addressSign==2)
                                address11 = addressCheckAdapter.getItem(position).getId();
                            else if (addressSign==3)
                                address111 = addressCheckAdapter.getItem(position).getId();
                            else if (addressSign==4)
                                address1ph = addressCheckAdapter.getItem(position).getId();
                            signAddress = 2;
                            province = tv_1.getText().toString().trim();
                            isIsgn = false;
                            mPresenter.onCityListData(addressCheckAdapter.getItem(position).getId());
                        } else if (signAddress == 2) {
                            tv_2.setVisibility(View.VISIBLE);
                            tv_2.setText(addressCheckAdapter.getItem(position).getName());
                            if (addressSign==1)
                                address2 = addressCheckAdapter.getItem(position).getId();
                            else if (addressSign==2)
                                address22 = addressCheckAdapter.getItem(position).getId();
                            else if (addressSign==3)
                                address222 = addressCheckAdapter.getItem(position).getId();
                            else if (addressSign==4)
                                address2ph = addressCheckAdapter.getItem(position).getId();
                            city = tv_2.getText().toString().trim();
                            signAddress = 3;
                            mPresenter.onCityListData(addressCheckAdapter.getItem(position).getId());
                        } else if (signAddress == 3) {
                            tv_3.setVisibility(View.VISIBLE);
                            tv_3.setText(addressCheckAdapter.getItem(position).getName());
                            if (addressSign==1)
                                address3 = addressCheckAdapter.getItem(position).getId();
                            else if (addressSign==2)
                                address33 = addressCheckAdapter.getItem(position).getId();
                            else if (addressSign==3)
                                address3333 = addressCheckAdapter.getItem(position).getId();
                            else if (addressSign==4)
                                address3ph = addressCheckAdapter.getItem(position).getId();
                            area = tv_3.getText().toString().trim();
//                            etAdd.setText(province + city + area);
                            signAddress = 4;
                            isIsgn = false;
                            mPresenter.onCityListData(addressCheckAdapter.getItem(position).getId());
                        } else {
                            tv_4.setVisibility(View.VISIBLE);
                            tv_4.setText(addressCheckAdapter.getItem(position).getName());
                            if (addressSign==1)
                                address4 = addressCheckAdapter.getItem(position).getId();
                            else if (addressSign==2)
                                address44 = addressCheckAdapter.getItem(position).getId();
                            else if (addressSign==3)
                                address4444 = addressCheckAdapter.getItem(position).getId();
                            else if (addressSign==4)
                                address4ph = addressCheckAdapter.getItem(position).getId();
                            street = tv_4.getText().toString().trim();
                            if (addressSign==1)
                                tvAddress.setText(area );
                            else if (addressSign==2)
                                tvAddres2.setText(area );
                            else if (addressSign==3)
                                tvAddres3.setText(area);
                            else if (addressSign==4)
                                tvPhAddress.setText(province + city + area + street);
                            popupWindowAddress.dismiss();
                        }

                    }
                });

                view.findViewById(R.id.tv_btn_colce).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindowAddress.dismiss();
                    }
                });
                break;
        }
    }


    /**
     * 上传 身份证正反和营业执照
     * @param index
     */
    private void imgD(int index){
        PictureSelector.create(CooperationActivity.this)
                .openGallery(PictureMimeType.ofImage())
                .loadImageEngine(GlideEngine.createGlideEngine())
                .theme(themeId)
                .selectionMode(PictureConfig.SINGLE)
                .imageSpanCount(4)
                .previewImage(true)
                .isCamera(true)
                .enableCrop(true)// 是否裁剪
                .compress(true)// 是否压缩
                .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                .scaleEnabled(true)
                .withAspectRatio(18, 18)
                .minimumCompressSize(100)// 小于100kb的图片不压缩
                .forResult(index);
    }

    /**
     * 上传
     */
    private void upAdFile() {
        if (list.size() > 0) {
            String time = TimeUtils.getTime10();
            PostRequest postRequest = OkGo.<UpImgBean>post(Api.API_UP_IMG);
            postRequest.params("type", "head");
            postRequest.params("file[]", new File(list.get(0)));
            postRequest.execute(new CacheCallBack<UpImgBean>() {
                @Override
                public void onSuccess(Response<UpImgBean> response) {
                    super.onSuccess(response);
                    UpImgBean bean = response.body();
                    if (bean.getCode() == Constants.HTTP_SUCCESS) {
                        strPics.add(bean.getMsg() + "/" + bean.getResult().get(0));
                        listIds.add(bean.getResult().get(0));
//                        LoggerUtils.e("图片的id：" + bean.getData().getId() + "图片的链接;" + bean.getData().getOriginal());
                        list.remove(0);
                        upAdFile();
                    } else if (bean.getCode() == Constants.HTTP_AGAIN_LOGIN) {
                        onReLoggedIn(response.body().getMsg());
                    } else {
                        RxToast.error(bean.getMsg());
                        list.remove(0);
                        upAdFile();
                    }

                }
            });
        }
    }
    /**
     * 3个切换
     * 公司 个人。 个体
     * @param index
     */
    private void init(int index){
        switch (index){
            case 1:
                identity=1;
                imgCheck1.setImageResource(R.drawable.check_true);
                tvStr1.setTextColor(getResources().getColor(R.color.main_color));

                imgCheck2.setImageResource(R.drawable.check_false);
                tvStr2.setTextColor(getResources().getColor(R.color.font_black));

                imgCheck3.setImageResource(R.drawable.check_false);
                tvStr3.setTextColor(getResources().getColor(R.color.font_black));
                tvName.setText("公司名称");
                etName.setHint("请输入公司名称");
                tvCard.setText("公司(信用代码)");
                etCard.setHint("请输入公司(信用代码)");
                img_3.setImageResource(R.drawable.yingye);

                break;
            case 2:
                identity=2;
                imgCheck1.setImageResource(R.drawable.check_false);
                tvStr1.setTextColor(getResources().getColor(R.color.font_black));

                imgCheck2.setImageResource(R.drawable.check_true);
                tvStr2.setTextColor(getResources().getColor(R.color.main_color));

                imgCheck3.setImageResource(R.drawable.check_false);
                tvStr3.setTextColor(getResources().getColor(R.color.font_black));
                tvName.setText("公司名称");
                etName.setHint("请输入公司名称");
                tvCard.setText("个体工商(注册号)");
                etCard.setHint("请输入个体工商(注册号)");
                img_3.setImageResource(R.drawable.yingye);
                break;
            case 3:
                identity=3;
                imgCheck1.setImageResource(R.drawable.check_false);
                tvStr1.setTextColor(getResources().getColor(R.color.font_black));

                imgCheck2.setImageResource(R.drawable.check_false);
                tvStr2.setTextColor(getResources().getColor(R.color.font_black));

                imgCheck3.setImageResource(R.drawable.check_true);
                tvStr3.setTextColor(getResources().getColor(R.color.main_color));
                tvName.setText("个人");
                etName.setHint("请输入姓名");
                tvCard.setText("个人(身份证号)");
                etCard.setHint("请输入个人(身份证号)");
                img_3.setImageResource(R.drawable.shefnez);
                break;
        }
    }
}
